package com.weina.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weina.entity.Employee;
import com.weina.entity.Menu;
import com.weina.service.MenuService;
import com.weina.utils.*;
import com.weina.vo.MenuVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("/admin/menu")
public class MenuController {
    @Resource
    private MenuService menuService;

    //加载左侧导航
    @RequestMapping("/loadMenuList")
    public String loadMeunList(HttpSession session){
        //创建Map集合，保存 MenuInfo菜单信息
        Map<String, Object> map = new LinkedHashMap<String,Object>();
        //创建Map集合，保存homeInfo信息
        Map<String, Object> homeInfo = new LinkedHashMap<>();
        //创建Map集合，保存logoInfo信息
        Map<String, Object> logoInfo = new LinkedHashMap<>();

        ///List<Menu> menuList = menuService.findMenuList();//该方法无论那个角色的用户登录，查询所有菜单的列表
        Employee employee = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        //根据当前用户的角色动态显示菜单列表
        List<Menu> menuList=menuService.findMenuListByEmployeeId(employee.getId());
        List<MenuNode> menuNodeList = new ArrayList<>();
        for(Menu m:menuList){
            MenuNode menuNode = new MenuNode();
            menuNode.setHref(m.getHref());
            menuNode.setIcon(m.getIcon());
            menuNode.setId(m.getId());
            menuNode.setPid(m.getPid());
            menuNode.setSpread(m.getSpread());
            menuNode.setTarget(m.getTarget());
            menuNode.setTitle(m.getTitle());
            menuNodeList.add(menuNode);
        }
        homeInfo.put("title","首页");
        homeInfo.put("href","/admin/desktop");

        logoInfo.put("title","黄河管理系统");
        logoInfo.put("image","/statics/layui/images/logo.png");
        logoInfo.put("href","/admin/home.html");

        map.put("menuInfo", TreeUtil.toTree(menuNodeList,0));
        map.put("homeInfo",homeInfo);
        map.put("logoInfo",logoInfo);

        return JSON.toJSONString(map);
    }
    @RequestMapping("/loadMenuTree")
    public DataGridViewResult loadMenuTree(){
        List<Menu> menuList = menuService.findMenuList();
        //创建集合保存节点数据
        List<TreeNode> treeNodes = new ArrayList<>();
        //循环遍历菜单列表集合
        for(Menu menu:menuList){
            Boolean spread=(menu.getSpread()==null || menu.getSpread()==1) ? true : false;
            treeNodes.add(new TreeNode(menu.getId(),menu.getPid(),menu.getTitle(),spread));
        }
        return new DataGridViewResult(treeNodes);
    }
    @RequestMapping("/list")
    public DataGridViewResult list(MenuVo menuVo){
        PageHelper.startPage(menuVo.getPage(),menuVo.getLimit());
        List<Menu> list=menuService.findMenuListByPage(menuVo);
        PageInfo<Menu> page = new PageInfo<Menu>(list);
        return new DataGridViewResult(page.getTotal(),page.getList());
    }
    //添加菜单方法
    @RequestMapping("/addMenu")
    public String addMenu(Menu menu){
        Map<String,Object> map = new HashMap<String,Object>();
        if(menuService.addMenu(menu)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"成功添加菜单");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加菜单失败");
        }
        return JSON.toJSONString(map);
    }
    //修改菜单方法
    @RequestMapping("/updateMenu")
    public String updateMenu(Menu menu){
        Map<String,Object> map = new HashMap<String,Object>();
        if(menuService.updateMenu(menu)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"菜单修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"菜单修改失败");
        }
        return JSON.toJSONString(map);
    }

    //检测是否有子菜单
    @RequestMapping("/checkMenuHasChild")
    public String checkMenuHasChild(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        if(menuService.checkMenuHasChild(id)>0){
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"该菜单下面还有子菜单，不能删除");
        }else{
            map.put(SystemConstant.EXIST,false);
        }
        return JSON.toJSONString(map);
    }
    //删除菜单方法
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        if(menuService.deleteMenuById(id)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }
}
